package top.kaoshanji.p2ch06b;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 代码清单 p2ch06b-02：捕获分组的名称作为后向引用的示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/17 19:47
 */
public class RegexExample02 {

    private static final Logger logger = LogManager.getLogger(RegexExample02.class);

    public static void main(String[] args) {
        logger.info("...代码清单 p2ch06b-02：捕获分组的名称作为后向引用的示例...");

        String str = "123-123-12-456-456";
        Pattern pattern = Pattern.compile("(?<num>\\d+)-\\k<num>");
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            String repeat = matcher.group("num");
            logger.info("...repeat:{}...", repeat);
        }


    }

}
